package com.white.algebra.controller;

import com.white.algebra.domain.Permission;
import com.white.algebra.domain.Role;
import com.white.algebra.service.RoleService;
import com.white.algebra.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class RoleController extends BaseServlet{
    //查询所有角色
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        RoleService roleService=new RoleServiceImpl();
        List<Role> roles=roleService.findAll();
        req.setAttribute("roles",roles);
        req.getRequestDispatcher("/pages/role-list.jsp").forward(req,resp);
    }
    //根据角色id查询对应的角色与对应的资源权限
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        RoleService roleService=new RoleServiceImpl();
        String id=req.getParameter("id");
        List<Role> roles = roleService.findById(id);
        req.setAttribute("role",roles);
        req.getRequestDispatcher("/pages/role-show.jsp").forward(req,resp);
    }
    //根据角色查询对应的资源权限
    protected void findUserByIdAndAllRole(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        RoleService roleService=new RoleServiceImpl();
        String id=req.getParameter("id");
        List<Permission> permission = roleService.findUserByIdAndAllRole(id);
        req.setAttribute("permission",permission);
        List<Role> roles = roleService.findById(id);
        req.setAttribute("role",roles);
        req.getRequestDispatcher("/pages/role-addpermission.jsp").forward(req,resp);
    }
    //关联角色与资源权限
    protected void addpermission(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        RoleService roleService=new RoleServiceImpl();
        String permissionId=req.getParameter("permissionId");
        String roleId=req.getParameter("roleId");
        roleService.addpermission(permissionId,roleId);
//        List<Permission> permission = roleService.findPermissionByRoleId(roleId);
//        req.setAttribute("permission",permission);
//        req.setAttribute("role",roleId);
        resp.sendRedirect(req.getHeader("Referer"));
    }
    //添加角色
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        RoleService roleService=new RoleServiceImpl();
        String roleName=req.getParameter("roleName");
        String roleDesc=req.getParameter("roleDesc");
        Role role=new Role();
        role.setRoleName(roleName);
        role.setRoleDesc(roleDesc);
        roleService.save(role);
        resp.sendRedirect(req.getContextPath()+"/role?action=findAll");
    }
    //根据角色id-删除角色，与关联表
    protected void deleteById(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        RoleService roleService=new RoleServiceImpl();
        String roleId=req.getParameter("id");
        roleService.deleteRole_PermissionById(roleId);
        roleService.deleteUserAndRoleById(roleId);
        roleService.deleteById(roleId);
        resp.sendRedirect(req.getContextPath()+"/role?action=findAll");
    }
    //根据角色id和资源权限id-删除角色_资源权限的关联
    protected void deletePermissionById(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        RoleService roleService=new RoleServiceImpl();
        String permissionId=req.getParameter("permissionId");
        String roleId=req.getParameter("roleId");
        roleService.deletePermissionById(permissionId,roleId);
        //List<Role> role = roleService.findById(roleId);
        resp.sendRedirect(req.getHeader("Referer"));
    }
}
